{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Title: #Count Largest Group"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Difficulty: #Easy"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Category Title: #Algorithms"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Tag Slug: #hash-table #math"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Name Translated: #哈希表 #数学"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solution Name: countLargestGroup"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Title: #统计最大组的数目"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Translated Content:\n",
    "<p>给你一个整数 <code>n</code>&nbsp;。请你先求出从 <code>1</code>&nbsp;到 <code>n</code> 的每个整数 10 进制表示下的数位和（每一位上的数字相加），然后把数位和相等的数字放到同一个组中。</p>\n",
    "\n",
    "<p>请你统计每个组中的数字数目，并返回数字数目并列最多的组有多少个。</p>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>示例 1：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 13\n",
    "<strong>输出：</strong>4\n",
    "<strong>解释：</strong>总共有 9 个组，将 1 到 13 按数位求和后这些组分别是：\n",
    "[1,10]，[2,11]，[3,12]，[4,13]，[5]，[6]，[7]，[8]，[9]。总共有 4 个组拥有的数字并列最多。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 2：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 2\n",
    "<strong>输出：</strong>2\n",
    "<strong>解释：</strong>总共有 2 个大小为 1 的组 [1]，[2]。\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 3：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 15\n",
    "<strong>输出：</strong>6\n",
    "</pre>\n",
    "\n",
    "<p><strong>示例 4：</strong></p>\n",
    "\n",
    "<pre><strong>输入：</strong>n = 24\n",
    "<strong>输出：</strong>5\n",
    "</pre>\n",
    "\n",
    "<p>&nbsp;</p>\n",
    "\n",
    "<p><strong>提示：</strong></p>\n",
    "\n",
    "<ul>\n",
    "\t<li><code>1 &lt;= n &lt;= 10^4</code></li>\n",
    "</ul>\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Description: [count-largest-group](https://leetcode.cn/problems/count-largest-group/description/)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Solutions: [count-largest-group](https://leetcode.cn/problems/count-largest-group/solutions/)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "test_cases = ['13', '2']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        l = [0]*37\n",
    "        for i in range(1, n+1):\n",
    "            num = 0\n",
    "            while i:\n",
    "                num += i%10\n",
    "                i = i // 10\n",
    "            l[num] += 1\n",
    "        l.sort()\n",
    "        tmp, res = l[-1], 1\n",
    "        for j in range(2,len(l)):\n",
    "            if l[len(l)-j] == tmp:\n",
    "                res += 1\n",
    "            else:\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        mp = Counter()\n",
    "        for i in range(1, n+1):\n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            mp[key] += 1\n",
    "        maxVal = max(mp.values())\n",
    "        return sum(1 for v in mp.values() if v == maxVal)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        c = Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            count = sum([int(j) for j in str(i)])\n",
    "            c[count] += 1\n",
    "        mx = max(c.values())\n",
    "        ans = sum(1 for v in c.values() if v == mx)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        zd = dict()\n",
    "        for i in range(1, n+1):\n",
    "            x = sum(int(i) for i in str(i))\n",
    "            zd[x] = zd.get(x, 0) + 1\n",
    "        x = max(zd.values())\n",
    "        return sum(i==x for i in zd.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        l = [0]*37\n",
    "        for i in range(1, n+1):\n",
    "            num = 0\n",
    "            while i:\n",
    "                num += i%10\n",
    "                i = i // 10\n",
    "            l[num] += 1\n",
    "        l.sort()\n",
    "        return l.count(l[-1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        def sum_of_digits(num):\n",
    "            sum=num%10\n",
    "            while num>=10:\n",
    "                num=num//10\n",
    "                sum+=num%10\n",
    "            return sum\n",
    "        counter=dict()\n",
    "        for i in range(1,n+1):\n",
    "            counter[sum_of_digits(i)]=counter.get(sum_of_digits(i),0)+1\n",
    "        max_sum=0\n",
    "        for key in counter:\n",
    "            max_sum=max(max_sum,counter[key])\n",
    "        ans=0\n",
    "        for key in counter:\n",
    "            if counter[key]==max_sum:\n",
    "                ans+=1\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        dic = defaultdict(int)\n",
    "        for i in range(1, n + 1):\n",
    "            tmp = [int(item) for item in str(i)]\n",
    "            dic[sum(tmp)] += 1\n",
    "        tmp = [item for item in dic.values()]\n",
    "\n",
    "        return tmp.count(max(tmp))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        dic = defaultdict(int)\n",
    "        for i in range(1, n + 1):\n",
    "            dic[sum([int(item) for item in str(i)])] += 1\n",
    "\n",
    "        tmp = [item for item in dic.values()]\n",
    "        return tmp.count(max(tmp))\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashmap = collections.Counter()\n",
    "        for i in range(1,n+1):\n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashmap[key] += 1\n",
    "        maxvalue = max(hashmap.values())\n",
    "        count = sum(1 for v in hashmap.values() if v==maxvalue)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "from typing import Counter\n",
    "\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans = 0\n",
    "        c = Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            count = sum([int(j) for j in str(i)])\n",
    "            c[count] += 1\n",
    "        mx = max(c.values())\n",
    "        ans = sum(1 for v in c.values() if v == mx)\n",
    "        return ans\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        S = [0 for _ in range(n + 1)]\n",
    "        C = [0 for _ in range(37)]\n",
    "        \n",
    "        for i in range(1, n + 1):\n",
    "            S[i] = S[i // 10] + i % 10\n",
    "            C[S[i]] += 1\n",
    "        \n",
    "        x = max(C)\n",
    "        return sum(i == x for i in C)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashmap = collections.Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            key = sum(int(x) for x in str(i))\n",
    "            hashmap[key] += 1\n",
    "        maxValue = max(hashmap.values())\n",
    "        count = sum(1 for v in hashmap.values() if v == maxValue)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        return len(multimode(sum(map(int, str(i))) for i in range(1, n+1)))\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        mem_dict = {}\n",
    "        for num in range(1,n+1):\n",
    "            sum_result = sum(int(i) for i in format(num, 'd'))\n",
    "            if mem_dict.get(sum_result):\n",
    "                mem_dict[sum_result] += 1\n",
    "            else:\n",
    "                mem_dict[sum_result] = 1\n",
    "        mem_lst = list(mem_dict.values())\n",
    "        mem_lst.sort()\n",
    "        return mem_lst.count(mem_lst[-1])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    " \n",
    "        for j in range(1,  n + 1):\n",
    "            k = sum(map(lambda x: int(x), list(str(j))))\n",
    "            c[k] += 1\n",
    "\n",
    "   \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        S = [0 for _ in range(n + 1)]\n",
    "        C = [0 for _ in range(37)]\n",
    "        \n",
    "        for i in range(1, n + 1):\n",
    "            S[i] = S[i // 10] + i % 10\n",
    "            C[S[i]] += 1\n",
    "        \n",
    "        x = max(C)\n",
    "        return sum(i == x for i in C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        l = [0]*37\n",
    "        for i in range(1, n+1):\n",
    "            num = 0\n",
    "            while i:\n",
    "                num += i%10\n",
    "                i = i // 10\n",
    "            l[num] += 1\n",
    "        l.sort()\n",
    "        tmp, res = l[-1], 1\n",
    "        for j in range(2,len(l)):\n",
    "            if l[len(l)-j] == tmp:\n",
    "                res += 1\n",
    "            else:\n",
    "                break\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        zd = dict()\n",
    "        for i in range(1, n+1):\n",
    "            x = 0\n",
    "            while i:\n",
    "                x += i%10\n",
    "                i //= 10\n",
    "            zd[x] = zd.get(x, 0) + 1\n",
    "        x = max(zd.values())\n",
    "        return sum(i==x for i in zd.values())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        dt = dict()\n",
    "        for i in range(1, n+1):\n",
    "            _sum = sum([int(x) for x in str(i)])\n",
    "            v = dt.get(_sum, 0) + 1\n",
    "            dt[_sum] = v\n",
    "        _max  = max(dt.values())\n",
    "        ct = sum(1 if v == _max else 0 for k,v in dt.items())\n",
    "        return ct"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        value_lst = list(hashMap.values())\n",
    "        maxValue = max(value_lst)\n",
    "        return value_lst.count(maxValue)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans=[0 for i in range(40)]\n",
    "        for i in range(1,n+1):\n",
    "            ans[sum(int(x) for x in list(str(i)))]+=1\n",
    "        return ans.count(max(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans = 1\n",
    "        list = [0 for _ in range(37)]\n",
    "        for i in range(1,n+1):\n",
    "            sum = 0\n",
    "            for j in range(len(str(i))):\n",
    "                sum += int(str(i)[j])\n",
    "            list[sum] += 1\n",
    "        list_sort = sorted(list, reverse=True)\n",
    "        for i in range(len(list) - 1):\n",
    "            if list_sort[i+1] == list_sort[0]:\n",
    "                ans += 1\n",
    "            else:\n",
    "                break\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashmap = collections.Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            key = sum(int(x) for x in str(i))\n",
    "            hashmap[key] += 1\n",
    "        maxValue = max(hashmap.values())\n",
    "        count = sum(1 for v in hashmap.values() if v == maxValue)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        c = [0] * 37\n",
    "        for i in range(1, n + 1):\n",
    "            t = 0\n",
    "            while i > 0:\n",
    "                t += i % 10\n",
    "                i //= 10\n",
    "            c[t] += 1\n",
    "        return c.count(max(c))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        d = {}\n",
    "\n",
    "        for i in range(1, n+1):\n",
    "            sum_temp = 0\n",
    "            for x in str(i):\n",
    "                sum_temp += int(x)\n",
    "            d[sum_temp] = d.get(sum_temp, 0) + 1\n",
    "        \n",
    "        d_s = sorted(d.items(), key = lambda x : x[1], reverse = True)\n",
    "        print(d_s)\n",
    "        res = 0\n",
    "        for v in d.values():\n",
    "            if v == d_s[0][1]:\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        S = [0] * (n + 1)\n",
    "        C = [0] * 37\n",
    "        \n",
    "        for i in range(1, n + 1):\n",
    "            S[i] = S[i // 10] + i % 10\n",
    "            C[S[i]] += 1\n",
    "        \n",
    "        x = max(C)\n",
    "        return C.count(x)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        def count_sum(num):\n",
    "            num_s = str(num)\n",
    "            res = 0\n",
    "            for item in num_s:\n",
    "                res += int(item)\n",
    "            return res\n",
    "\n",
    "        num_dic = dict()\n",
    "        max_num = 0\n",
    "        for i in range(1, n+1):\n",
    "            cur_sum = count_sum(i)\n",
    "            if cur_sum not in num_dic.keys():\n",
    "                num_dic[cur_sum] = 1\n",
    "            else:\n",
    "                num_dic[cur_sum] += 1\n",
    "            if max_num < num_dic[cur_sum]:\n",
    "                max_num = num_dic[cur_sum]\n",
    "        \n",
    "        return list(num_dic.values()).count(max_num)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        c = Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            t = 0\n",
    "            while i > 0:\n",
    "                t += i % 10\n",
    "                i //= 10\n",
    "            c[t] += 1\n",
    "        cc = Counter(c.values())\n",
    "        return cc[max(cc)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        c = Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            s = sum(map(int, str(i)))\n",
    "            c[s] += 1\n",
    "        cc = Counter(c.values())\n",
    "        return cc[max(cc)]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        cnt=[0]*37\n",
    "        def s(x):\n",
    "            ans=0\n",
    "            while x>0:\n",
    "                ans+=x%10\n",
    "                x//=10\n",
    "            return ans\n",
    "        for i in range(1,n+1):\n",
    "            cnt[s(i)]+=1\n",
    "        return cnt.count(max(cnt))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans=[0 for i in range(40)]\n",
    "        for i in range(1,n+1):\n",
    "            ans[sum(int(x) for x in list(str(i)))]+=1\n",
    "        return ans.count(max(ans))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    " \n",
    "        for j in range(1,  n + 1):\n",
    "            k = sum(map(lambda x: int(x), list(str(j))))\n",
    "            c[k] += 1\n",
    "\n",
    "   \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        return max(Counter((Counter(map(lambda x: sum(map(int, str(x + 1))), range(n))).values())).items())[1]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        d = {}\n",
    "        for i in range(1, n+1):\n",
    "            sum_i = 0\n",
    "            for j in str(i):\n",
    "                sum_i += int(j)\n",
    "            d[sum_i] = d.get(sum_i, 0) + 1\n",
    "        res = list(d.values()).count(max(d.values()))\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        def he(x):\n",
    "            res = 0\n",
    "            x = str(x)\n",
    "            for i in range(len(x)):\n",
    "                res = res+int(x[i])\n",
    "            return res\n",
    "        counts = []\n",
    "        for i in range(1,n+1):\n",
    "            counts.append(he(i))\n",
    "        a = [counts.count(i) for i in set(counts)]\n",
    "        #return [counts,a]\n",
    "        '''max1 = 0\n",
    "        res = 0\n",
    "        for i in a:\n",
    "            if a.count(i)*i>max1:\n",
    "                max1 = a.count(i)*i\n",
    "                res = a.count(i)\n",
    "        return res'''\n",
    "        return a.count(max(a))\n",
    "        #return int(max(a.count(i)*i for i in a)/i)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        d = {} \n",
    "        for i in range(1,n+1):    \n",
    "            k = 0\n",
    "            for j in str(i):   #遍历每一位\n",
    "                k += int(j)     #将字符串转换为整数值\n",
    "            if k in d:\n",
    "                d[k] +=1\n",
    "            else:\n",
    "                d[k] = 1\n",
    "        return list(d.values()).count(max(d.values()))   #在生成的列表中统计和最大的次数\n",
    "\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        #统计所有从1到n的10进制整数数位和\n",
    "        #使用字典导出数字和对应数位和组别\n",
    "        #再统计每个数位和出现次数，统计并列最多的组别数目\n",
    "        \n",
    "        return max(Counter((Counter(map(lambda x:sum(map(int,str(x+1))),range(n))).values())).items())[1]\n",
    "\n",
    "\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        res = [0] * 37\n",
    "        ansmax = 0\n",
    "        ans = 0\n",
    "        for n in range(1, n+1):\n",
    "            t = 0\n",
    "            while n > 0:\n",
    "                t += n % 10\n",
    "                n = n // 10\n",
    "            res[t] += 1\n",
    "            ansmax = max(res[t], ansmax)\n",
    "\n",
    "        for i in res:\n",
    "            if i == ansmax:\n",
    "                ans += 1\n",
    "        return ans\n",
    "\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        d={}\n",
    "        for i in range(1,n+1):\n",
    "            k=0\n",
    "            for j in str(i):\n",
    "                k+=int(j)#转为字符串后得位数和\n",
    "            if k in d:\n",
    "                d[k]+=1\n",
    "            else:\n",
    "                d[k]=1\n",
    "        return list(d.values()).count(max(d.values()))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        c = Counter()\n",
    "        for i in range(1, n + 1):\n",
    "            t = 0\n",
    "            while i > 0:\n",
    "                t += i % 10\n",
    "                i //= 10\n",
    "            c[t] += 1\n",
    "        max_v = max(c.values())\n",
    "        return sum(v == max_v for v in c.values())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = collections.Counter()\n",
    "        for i in range(1,n+1):\n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] +=1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v==maxValue)\n",
    "        return count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        lst = [0 for _ in range(40)]\n",
    "        for i in range(1, n + 1):\n",
    "            s = 0\n",
    "            for c in str(i):\n",
    "                s += int(c)\n",
    "            lst[s] += 1\n",
    "        return lst.count(max(lst))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        S = [0 for _ in range(n + 1)]\n",
    "        C = [0 for _ in range(37)]\n",
    "        \n",
    "        for i in range(1, n + 1):\n",
    "            S[i] = S[i // 10] + i % 10\n",
    "            C[S[i]] += 1\n",
    "        \n",
    "        x = max(C)\n",
    "        return sum(i == x for i in C)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    " \n",
    "        for j in range(1,  n + 1):\n",
    "            k2 = 0\n",
    "            m = j\n",
    "            while m != 0:\n",
    "                k2 += m % 10\n",
    "                m //= 10\n",
    "\n",
    "            c[k2] += 1\n",
    "\n",
    "   \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        hashMap = Counter()\n",
    "        for i in range(1, n + 1): \n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            hashMap[key] += 1\n",
    "        maxValue = max(hashMap.values())\n",
    "        count = sum(1 for v in hashMap.values() if v == maxValue)\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans = [0 for i in range(37)]\n",
    "        for i in range(1, n + 1):\n",
    "            ans[sum([int(char) for char in str(i)])] += 1\n",
    "        c = collections.Counter(ans)\n",
    "        m = max(ans)\n",
    "        return c[m]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        mp = Counter()\n",
    "        for i in range(1, n+1):\n",
    "            key = sum([int(x) for x in str(i)])\n",
    "            mp[key] += 1\n",
    "        maxVal = max(mp.values())\n",
    "        return sum(1 for v in mp.values() if v == maxVal)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        #计数排序，思路，10^4=10000，计数最大，9999=36\n",
    "        def calcX(x: int) -> int:\n",
    "            res = 0\n",
    "            while x > 0:\n",
    "                res += x % 10\n",
    "                x = x // 10\n",
    "            return res\n",
    "        \n",
    "        # counter中存储的是，每个组的数目\n",
    "        counter = [0] * 37\n",
    "        for i in range(1, n + 1):\n",
    "            cnt = calcX(i)\n",
    "            counter[cnt] += 1\n",
    "        \n",
    "        print(counter)\n",
    "        # 数字并列的组最多\n",
    "        maxarr = max(counter)\n",
    "        return Counter(counter)[maxarr]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ans = {}\n",
    "        for i in range(1,n+1):\n",
    "            res = 0\n",
    "            for j in str(i):\n",
    "                res += int(j)\n",
    "            \n",
    "            if(res not in ans):\n",
    "                ans[res] = 1\n",
    "            else:\n",
    "                 ans[res] += 1 \n",
    "        \n",
    "\n",
    "        ans = sorted(ans.items(), key=lambda x: x[1], reverse = True)\n",
    "        most = ans[0][1]\n",
    "        final = 0\n",
    "        for ce in ans:\n",
    "            if(ce[1] == most):\n",
    "                final+=1\n",
    "            else:\n",
    "                break\n",
    "        return final\n",
    "\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    " \n",
    "        for j in range(1,  n + 1):\n",
    "            k2 = 0\n",
    "            m = j\n",
    "            while m != 0:\n",
    "                k2 += m % 10\n",
    "                m //= 10\n",
    "\n",
    "            c[k2] += 1\n",
    "\n",
    "   \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        dict1={}\n",
    "        for i in range(1,n+1):\n",
    "            temp=[i for i in str(i)]\n",
    "            temp=[int(i) for i in temp]\n",
    "            sum1=sum(temp)\n",
    "            if sum1 in dict1:\n",
    "                dict1[sum1]+=1\n",
    "            else:\n",
    "                dict1[sum1]=1\n",
    "        bb=[j for i,j in dict1.items()]\n",
    "        maxx=max(bb)\n",
    "        cc=[i for i in bb if i==maxx]\n",
    "        return len(cc)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        tmp = []\n",
    "        for i in range(1,n+1):  # 遍历从1到n的整数\n",
    "            s = 0\n",
    "            for j in str(i):   # 转换为字符串，遍历所有数位\n",
    "                s+=int(j)   # 转为int类型，计算数位和\n",
    "            tmp.append(s)  # 将数位和放入队列tmp\n",
    "        c = Counter(tmp)  # 统计tmp形成字典c\n",
    "        v = 0\n",
    "        res = 1\n",
    "        for key in c.keys():  # 遍历字典c\n",
    "            if c[key]>v:  # 与临时最大value值v进行比较\n",
    "                v=c[key]  # 记录当前最大value值v\n",
    "                res = 1  # \n",
    "            elif c[key]==v:res+=1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        res={}\n",
    "        count=0\n",
    "    \n",
    "        for i in range(1,n+1):\n",
    "            temp=sum(map(int,list(str(i))))\n",
    "            if temp not in res:\n",
    "                res[temp]=[]\n",
    "            res[temp].append(i)\n",
    "            count=max(count,len(res[temp]))\n",
    "        #print(count)\n",
    "        ans=sum([1 for vv in res.values() if len(vv)==count])\n",
    "        return ans"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    " \n",
    "        for j in range(1,  n + 1):\n",
    "            # k1 = sum(map(lambda x: int(x), list(str(j))))\n",
    "            k2 = 0\n",
    "            m = j\n",
    "            while m != 0:\n",
    "                k2 += m % 10\n",
    "                m //= 10\n",
    "\n",
    "            c[k2] += 1\n",
    "\n",
    "   \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    "        cnt, ans = 0, []\n",
    "        for i in range(1, n + 1):\n",
    "            k = 0\n",
    "            for j in range(1,  n + 1):\n",
    "                k = sum(map(lambda x: int(x), list(str(j))))\n",
    "                if k == i:\n",
    "                    c[i] += 1\n",
    "                    ans.append(c[i])\n",
    "\n",
    "            if len(ans) == n:\n",
    "                break\n",
    "                        \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\r\n",
    "    def countLargestGroup(self, n: int) -> int:\r\n",
    "        records = {}\r\n",
    "        for i in range(1, n + 1):\r\n",
    "            k = sum([int(x) for x in str(i)])\r\n",
    "            records[k] = records.get(k, 0) + 1\r\n",
    "\r\n",
    "        maxv = max(records.values())\r\n",
    "        return sum(1 for v in records.values() if v == maxv)\r\n",
    "\r\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def get_sum(self,num):\n",
    "        res=0\n",
    "        while num:\n",
    "            t=num%10\n",
    "            res+=t\n",
    "            num=num//10\n",
    "        return res\n",
    "\n",
    "\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        nums=[i for i in range(1,n+1)]\n",
    "        dic={}\n",
    "        for num in nums:\n",
    "            sum=self.get_sum(num)\n",
    "            # dic[num]=sum\n",
    "            dic[sum]=dic.get(sum,0)+1\n",
    "        # print(dic)\n",
    "\n",
    "        res=list(dic.values())\n",
    "        maxnum=max(res)\n",
    "        # print(maxnum)\n",
    "        # \n",
    "        # print(res.count(2))\n",
    "        return res.count(maxnum)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        dic = collections.defaultdict(list)\n",
    "        for i in range(1, n+1):\n",
    "            if len(str(i)) == 1:\n",
    "                dic[i].append(i)\n",
    "            else:\n",
    "                total = 0\n",
    "                for ch in list(str(i)):\n",
    "                    total += int(ch)\n",
    "                dic[total].append(i)\n",
    "        res = 0\n",
    "        dic_ = {}\n",
    "        for key, value in dic.items():\n",
    "            dic_[key] = len(value)\n",
    "        for key, value in dic_.items():\n",
    "            if value == max(dic_.values()):\n",
    "                res += 1\n",
    "        return res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    "        cnt, ans = 0, []\n",
    "        for i in range(1, n + 1):\n",
    "            for j in range(1,  n + 1):\n",
    "                k = sum(map(lambda x: int(x), list(str(j))))\n",
    "                if k == i:\n",
    "                    c[i] += 1\n",
    "                    ans.append(c[i])\n",
    "\n",
    "            if len(ans) == n:\n",
    "                break\n",
    "                        \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        # 1 - 10000\n",
    "        tmp_dict = dict()\n",
    "\n",
    "        for i in range(1, n+1):\n",
    "            tmp = sum([int(j) for j in str(i)])\n",
    "\n",
    "            if tmp not in tmp_dict:\n",
    "                tmp_dict[tmp] = [i]\n",
    "            else:\n",
    "                tmp_list = tmp_dict[tmp]\n",
    "                tmp_list.append(i)\n",
    "                tmp_dict[tmp] = tmp_list\n",
    "\n",
    "        result_dict = dict()\n",
    "\n",
    "        for k, v in tmp_dict.items():\n",
    "            len_key = len(v)\n",
    "            if len_key not in result_dict:\n",
    "                result_dict[len_key] = 1\n",
    "            else:\n",
    "                result_dict[len_key] += 1\n",
    "                \n",
    "        return result_dict[max(result_dict)]\n",
    "\n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    "        cnt, ans = 0, []\n",
    "        for i in range(1, n + 1):\n",
    "            k = 0\n",
    "            for j in range(1,  n + 1):\n",
    "                k = sum(map(lambda x: int(x), list(str(j))))\n",
    "                if k == i:\n",
    "                    c[i] += 1\n",
    "                    ans.append(c[i])\n",
    "\n",
    "            if len(ans) == n:\n",
    "                break\n",
    "                        \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    "        cnt, ans = 0, []\n",
    "        for i in range(1, n + 1):\n",
    "            for j in range(1,  n + 1):\n",
    "                k = sum(map(lambda x: int(x), list(str(j))))\n",
    "                if k == i:\n",
    "                    c[i] += 1\n",
    "                    ans.append(c[i])\n",
    "\n",
    "            if len(ans) == n:\n",
    "                break\n",
    "                        \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ad={k:[] for k in range(1,45)}\n",
    "\n",
    "        for i in range(1,n+1):\n",
    "            if i<10:\n",
    "                ad[i].append(i)\n",
    "            elif i<100:\n",
    "                ad[i//10+i%10].append(i)\n",
    "            elif i<1000:\n",
    "                ad[i//100+i%100//10+i%100%10].append(i)\n",
    "            elif i<10000:\n",
    "                ad[i//1000+i%1000//100+i%1000%100//10+i%1000%100%10].append(i)\n",
    "            elif i<100000:\n",
    "                ad[i//10000+i%10000//1000+i%10000%1000//100+i%10000%1000%100//10+i%10000%1000%100%10].append(i)\n",
    "        #print(ad)\n",
    "        mlen,count=0,0\n",
    "        for k,v in ad.items():\n",
    "            mlen=max(mlen,len(v))\n",
    "        for k,v in ad.items():\n",
    "            if len(v)==mlen:\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "\n",
    "        c = Counter()\n",
    "        cnt, ans = 0, []\n",
    "        for i in range(1, n + 1):\n",
    "            for j in range(1,  n + 1):\n",
    "                k = sum(map(lambda x: int(x), list(str(j))))\n",
    "                if k == i:\n",
    "                    c[i] += 1\n",
    "                    ans.append(c[i])\n",
    "\n",
    "            if len(ans) == n:\n",
    "                print(len(ans), c)\n",
    "                break\n",
    "                        \n",
    "        cv = list(c.values())\n",
    "        max1 = max(cv)\n",
    "        return cv.count(max1)\n",
    "\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        if n < 10:\n",
    "            return n\n",
    "        else:\n",
    "            max_len = 0\n",
    "            d = {}\n",
    "            for i in range(1, n+1):\n",
    "                n_sum = 0\n",
    "                cur = i\n",
    "                while cur // 10 > 0:\n",
    "                    n_sum += cur % 10\n",
    "                    cur = cur // 10\n",
    "                n_sum += cur\n",
    "                if n_sum in d:\n",
    "                    d[n_sum].append(i)\n",
    "                else:\n",
    "                    d[n_sum] = [i]\n",
    "                if len(d[n_sum]) > max_len:\n",
    "                    max_len = len(d[n_sum])\n",
    "            cnt = 0\n",
    "            for ls in d.values():\n",
    "                if len(ls) == max_len:\n",
    "                    cnt += 1\n",
    "            return cnt \n",
    "            \n",
    "                "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "import collections\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        ad={k:[] for k in range(1,45)}\n",
    "\n",
    "        for i in range(1,n+1):\n",
    "            t=str(i)\n",
    "            s=0\n",
    "            for x in t:\n",
    "                s+=int(x)\n",
    "            ad[s].append(i)\n",
    "        #print(ad)\n",
    "        mlen,count=0,0\n",
    "        for k,v in ad.items():\n",
    "            mlen=max(mlen,len(v))\n",
    "        for k,v in ad.items():\n",
    "            if len(v)==mlen:\n",
    "                count+=1\n",
    "        return count\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from typing import List\n",
    "import collections\n",
    "\n",
    "class Solution:\n",
    "    def countLargestGroup(self, n: int) -> int:\n",
    "        dic = {}\n",
    "        for i in range(1,n+1):\n",
    "            sum = 0\n",
    "            x = i\n",
    "            while x > 0:\n",
    "               sum += x % 10\n",
    "               x //= 10\n",
    "            if sum not in dic:\n",
    "                dic[sum] = [i]\n",
    "            else:\n",
    "                dic[sum] += [i]\n",
    "        arr1 = []\n",
    "        for a in dic:\n",
    "            arr1.append(dic[a])\n",
    "        arr1.sort(key=lambda x:len(x),reverse= True)\n",
    "        ans = 1\n",
    "        for i in range(1,len(arr1)):\n",
    "            if len(arr1[i]) != len(arr1[i-1]):\n",
    "                break\n",
    "            ans += 1\n",
    "        return ans\n",
    "        "
   ]
  }
 ],
 "metadata": {},
 "nbformat": 4,
 "nbformat_minor": 2
}
